<?php


namespace app\api\controller;

use app\admin\model\SaasBusiness;
use app\admin\model\SaasGoodsSpecs as SGS;
use app\admin\model\User;
use app\admin\model\SaasShopCart as SSC;
use app\admin\model\SaasGoods as SG;
use app\admin\model\SaasBusiness as SB;
use think\Request;

class SaasShopCart extends Base
{
    /***
     * @param Request $request
     * 添加购物车
     * user_id
     * goods_id
     * is_group_buying
     * specs_id
     * goods_price
     */
    public function addSaasCart(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['user_id']) or
            !isset($data['goods_id']) or
            !isset($data['is_group_buying']) or
            !isset($data['specs_id']) or
            !isset($data['goods_num']) or
            !isset($data['goods_price'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $sg = new SG();
        $whereGoods['id'] = $data['goods_id'];
        $isGoods = $sg->getOne($whereGoods);
        if(!$isGoods){
            $res['data']['code'] = 0;
            $res['data']['msg'] = "无此商品信息";
            echo json_encode($res);die;
        }

        //判断购物车中是否有此商品
        $whereCart['user_id'] = $data['user_id'];
        $whereCart['goods_id'] = $data['goods_id'];
        $whereCart['specs_id'] = $data['specs_id'];
        $whereCart['is_group_buying'] = $data['is_group_buying'];
        $ssg = new SSC();
        $incart = $ssg->getOne($whereCart);
        if(!$incart){
            //不存在购物车中
            $addData['user_id'] = $data['user_id'];
            $addData['goods_id'] = $data['goods_id'];
            $addData['goods_number'] = $data['goods_num'];
            $addData['is_group_buying'] = $data['is_group_buying'];
            $addData['specs_id'] = $data['specs_id'];
            $addData['b_id'] = $isGoods['b_id'];
            //后期需要验证价格
            $addData['goods_price'] = $data['goods_price'];
            $rs = $ssg->add($addData);
        }else{
            //存在购物车中
            $editWhere['id'] = $incart['id'];
            $editData['goods_number'] = $incart['goods_number']+$data['goods_num'];
            $rs = $ssg->edit($editWhere,$editData);
        }

        if($rs){
            $res['data']['code'] = 1;
            $res['data']['msg'] = "添加购物车成功";
        }else{
            $res['data']['code'] = 0;
            $res['data']['msg'] = "添加购物车失败";
        }
        $res['status'] = 200;
        echo json_encode($res);

//        //判断是否为团购1:非团购，2：团购
//        if($data['is_group_buying'] == 2){
//            //是团购需要确定购买数量范围而定价，
//        }else{
//            //不是团购直接按照单品价格结算
//        }
    }

    /***
     * @param Request $request
     * 购物车追加数量
     */
    public function addSaasNumber(Request $request){
        $res = [];
        $data = $request->post();

        if(!isset($data['user_id']) or !isset($data['cart_id']) or !isset($data['goods_number'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $ssc = new SSC();
        //是否存在
        $whereSsc['id'] = $data['cart_id'];
        $whereSsc['user_id'] = $data['user_id'];
        $incart = $ssc->getOne($whereSsc);
        if(!$incart){
            $res['data']['code'] = 0;
            $res['data']['msg'] = "无此商品信息";
        }else{
            $editData['goods_number'] = $data['goods_number'];
            if($ssc->edit($whereSsc,$editData)){
                $res['data']['code'] = 1;
                $res['data']['msg'] = "追加成功";
            }else{
                $res['data']['code'] = 0;
                $res['data']['msg'] = "追加失败";
            }
        }
        $res['status'] = 200;
        echo json_encode($res);
    }

    /***
     * @param Request $request
     * 用户购物车列表
     * user_id
     */
    public function userSaasCartList(Request $request){
        $res = [];
        $data = $request->post();

        if(!isset($data['user_id'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $ssc = new SSC();
        $where['user_id'] = $data['user_id'];
        $rs = $ssc->getAll($where);
        $sg = new SG();
        $sb = new SaasBusiness();
        $sgs = new SGS();
        foreach ($rs as $k=>$v){
            //商品信息
            $whereSg['id'] = $v['goods_id'];
            $info = $sg->getOne($whereSg);
            $rs[$k]['goods_info'] = $info;
            //商家信息
            $whereSb['id'] = $info['b_id'];
            $binfo = $sb->getOne($whereSb);
            $rs[$k]['goods_info']['b_name'] = $binfo['name'];
            //规格信息
            $whereSgs['id'] = $v['specs_id'];
            $sgsInfo = $sgs->getOne($whereSgs);
            $rs[$k]['specs_info'] = $sgsInfo;
        }
        if($rs){
            $res['data']['code'] = 1;
            $res['data']['msg'] = "查询成功";
            $res['data']['list'] = $rs;
        }else{
            $res['data']['code'] = 0;
            $res['data']['msg'] = "查询失败";
        }
        $res['status'] = 200;
        echo json_encode($res);
    }

    /***
     * @param Request $request
     * 购物车结算成订单
     * user_id
     * cart_id
     */
    public function saasCartToSaasOrder(Request $request){
        $res = [];
        $data = $request->post();

        if(!isset($data['user_id']) or !isset($data['cart_id'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }


        $ssc = new SSC();
        $sb = new SB();
        $sg = new SG();
        //是否存在
        $whereSsc['id'] = ['in',explode(',',$data['cart_id'])];
        $whereSsc['user_id'] = $data['user_id'];
        $rs = $ssc->getAll($whereSsc);
        foreach ($rs as $k=>$v){
            $whereSb['id'] = $v['b_id'];
            $sbrs = $sb->getOne($whereSb);
            $rs[$k]['b_name'] = $sbrs['name'];
            $whereSg['id'] = $v['goods_id'];
            $sgrs = $sg->getOne($whereSg);
            $rs[$k]['goods_img'] = $sgrs['img'];
            $rs[$k]['goods_name'] = $sgrs['goods_name'];
        }

        if($rs){
            $res['data']['code'] = 1;
            $res['data']['msg'] = "创建订单成功";
            $res['data']['list'] = $rs;
            foreach(explode(',',$data['cart_id']) as $v){
                $whereDel['id'] = $v;
                $ssc->del($whereDel);
            }

        }else{
            $res['data']['code'] = 0;
            $res['data']['msg'] = "创建订单失败，商品信息有误";
        }
        $res['status'] = 200;
        echo json_encode($res);
    }
}

